//
//  ValidationManageViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/2.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "ValidationManageViewController.h"
#import "OrderCalendarView.h"
#import "ValidationCell.h"
#import "ValidationModel.h"
#import "ValidationManageViewModel.h"
#import "CommonMethod.h"
@interface ValidationManageViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UILabel *calendarLab;
@property (nonatomic ,strong) NSMutableArray *dataSource;
@property (nonatomic ,assign) NSInteger pagNum;
@end

@implementation ValidationManageViewController

- (NSMutableArray *)dataSource {

    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"验证记录";
    _userInfo = [UserInfo sharedUserInfo];
    [self setNarBarUI];
    [self createTableViewUI];
//    [self loadDataSecretKeyList];
//    [self MJRefreshUI];
//    [self.tableView.mj_header beginRefreshing];

    
}

- (void)loadDataSecretKeyList {

    
    ValidationManageViewModel *validationViewModel = [[ValidationManageViewModel alloc] init];
    
    // 商家编号
    NSString *bh = _userInfo.bh;
    // 页数
    self.pagNum = 1;
    // 时间
    NSString *sjStr = [NSString stringWithFormat:@"%@-1",self.calendarLab.text];
    [validationViewModel loadValidationListNetWorking:bh andPage:[NSString stringWithFormat:@"%ld",self.pagNum] andSj:sjStr andReturnValue:^(id returnValue) {
        self.dataSource = returnValue;
        if (self.dataSource.count == 0) {
            UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"什么都没有-1.jpg"]];
            backImage.frame = self.view.frame;
            [self.view addSubview:backImage];
        }
        [self.tableView reloadData];
    } andReturenError:^(id errorCode) {
        
    }];
}

- (void)setNarBarUI {

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rili"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemClicked:)];
    self.navigationItem.rightBarButtonItem = rightItem;

}

#pragma mark - 日历按钮
- (void)rightItemClicked:(UIBarButtonItem *)sender {
    
    NSLog(@"日历");
    OrderCalendarView *orderCanlendarView = [[OrderCalendarView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [orderCanlendarView showCustomCalendarView];
    orderCanlendarView.selectTime = ^(NSString *time){
        
        self.calendarLab.text = time;
    };
    
}



- (void)createTableViewUI {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, size_width, size_height - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.view addSubview:self.tableView];

    UIView *headerBackView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 36))];
    headerBackView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.tableView.tableHeaderView = headerBackView;
    
    // 背景色
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 8, size_width, 28))];
    headerView.backgroundColor = [UIColor whiteColor];
    [headerBackView addSubview:headerView];
    
    // 日期
    UILabel *calendarLab = [[UILabel alloc] initWithFrame:(CGRectMake(12, 8, 100, 12))];
    calendarLab.textColor = color_textTwo;
    calendarLab.text = [CommonMethod getDateNow:@"20YY-MM"];
    calendarLab.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:calendarLab];
    self.calendarLab = calendarLab;
    
    // 验证张数
    UILabel *totalLab = [[UILabel alloc] initWithFrame:(CGRectMake(calendarLab.frame.size.width  + calendarLab.frame.origin.x + 22, 8, 100, 12))];
    totalLab.textColor = color_textTwo;
    totalLab.font = [UIFont systemFontOfSize:12];
    NSString *numberStr = @"多少";
    totalLab.text = [NSString stringWithFormat:@"验证%@张",numberStr];
    NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:totalLab.text];
    [totalStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"ff9900"]} range:[totalLab.text rangeOfString:[NSString stringWithFormat:@"%@",numberStr]]];
    totalLab.attributedText = totalStr;
    [headerView addSubview:totalLab];
    
    // 底部线条
    UILabel *lineLab = [[UILabel alloc] initWithFrame:(CGRectMake(12, headerBackView.frame.size.height - 1, size_width, 1))];
    lineLab.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [headerBackView addSubview:lineLab];
    
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"ValidationCell" bundle:nil] forCellReuseIdentifier:@"ValidationCell"];
    
}

#pragma mark -- 上拉加载  下拉刷新
- (void)MJRefreshUI {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataSource.count) {
            [self.dataSource removeAllObjects];
        }
        [self loadDataSecretKeyList];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.pagNum++;
        
        [self loadDataSecretKeyList];
        
    }];
}



#pragma mark - UITableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ValidationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ValidationCell" forIndexPath:indexPath];
//    [cell setCellUpDataWithValidationModel:self.dataSource[indexPath.row]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84;
}

@end
